{% extends "html/base_authenticated.html" %}
{#
INPUT:
- pub -- a boolean stating whether to show in public mode.
- typ -- a string stating what kind of worksheets this listing shows
- worksheets -- list of Worksheet objects
#}
{% if pub %}
{% set worksheet_heading='Published Worksheets' %}
{% elif typ == 'trash' %}
{% set worksheet_heading='Deleted Worksheets' %}
{% elif typ == 'active' %}
{% set worksheet_heading='Active Worksheets' %}
{% else %}
{% set worksheet_heading='Archived Worksheets' %}
{% endif %}  

{% block title %}
{{ worksheet_heading }}
{% endblock %}

{% block page_id %}worksheet-listing-page{% endblock %}

{% block javascript %}
{% if not pub %}
<link rel="stylesheet" href="/javascript/jqueryui/css/sage/jquery-ui-1.7.2.custom.css" />
<script type="text/javascript" src="/javascript/jqueryui/js/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript" src="/javascript/jquery/plugins/form/jquery.form.min.js"></script>
<script type="text/javascript" src="/javascript/jquery/plugins/jquery.bgiframe.min.js"></script>
<script type="text/javascript">
    var worksheet_filenames = {{ worksheet_filenames|tojson|safe }}; 
</script>
<script type="text/javascript" src="/javascript/sage/main.js"></script>
<script type="text/javascript" src="/javascript/gears/gears_init.js"></script> 
<script type="text/javascript" src="/javascript/gears/shortcut.js"></script>
{% else %}
<script type="text/javascript" src="/javascript/sage/ws_list.js"></script>
{% endif %}
{% endblock %}


{% block main %}
<div id="user-main-controls" class="user-controls">
    {% if pub is not defined or not pub %}
    <a href="/new_worksheet" target="_blank">New Worksheet</a>
    <a href="/upload">Upload</a>
    <a href="/download_worksheets.zip">Download All Active</a>
    {% endif %}

    <div id="search-area">
        <form action="." method="GET">
            <input type="hidden" value="{{ typ if not pub else 'pub' }}" name="typ" />
            <input id="search-worksheets" size="20" value="{{ search if search else "" }}" name="search" />
            <button class="add_new_worksheet_menu" id="search-worksheets-button" type="submit">Search Worksheets</button>
        </form>
    </div>
</div>       
<div id="worksheet-list-controls" class="controls">
    {% if not pub %}
    <div class="action-buttons">
        {% if typ == 'archive' %}
        <button onClick="make_active_button();" title="Unarchive selected worksheets so it appears in the default worksheet list">Unarchive</button>
        {% else %}
        <button onClick="archive_button();" title="Archive selected worksheets so they do not appear in the default worksheet list">Archive</button>
        {% endif %}

        {% if typ != 'trash' %}
        <button onClick="delete_button();" title="Move the selected worksheets to the trash">Delete</button>
        {% else %}
        <button onClick="make_active_button();" title="Move the selected worksheets out of the trash">Undelete</button>
        {% endif %}

        <button onClick="stop_worksheets_button();" title="Stop selected worksheets">Stop</button>
        {% if not accounts %}
        <button onClick="download_worksheets_button();" title="Download selected worksheets">Download</button>
        {% endif %}
    </div>
    <div class="folders">
        Current Folder:
        <a class="{{ 'bold' if typ == 'active' else '' }}usercontrol" href=".">Active</a>
        <a class="{{ 'bold' if typ == 'archive' else '' }}usercontrol" href=".?typ=archive">Archived</a>
        <a class="{{ 'bold' if typ == 'trash' else '' }}usercontrol" href=".?typ=trash">Trash</a>

        {% if typ == 'trash' %}
        <form method="POST" action="/emptytrash" id="empty-trash-form">
            <a class="boldusercontrol" onClick="empty_trash(this); return false" href="#">(Empty Trash)</a>
        </form>
        {% endif %}
    </div>
    {% endif %}
</div>

<table cellspacing="0" cellpadding="0" id="worksheet-list">
    <thead>
        <tr>
            {% if not pub %}
            <td class="checkbox">
                <input id="controlbox" onClick="set_worksheet_list_checks();" class="entry" type="checkbox" />
            </td>
            {% else %}
            <td><a class="listcontrol" href=".?sort=rating">Rating</a></td>
            {% endif %}

            <td>
                <a class="listcontrol" href=".?typ={{ typ }}&sort=name{{ '' if sort != 'name' or reverse else '&reverse=True' }}">
                    {{ worksheet_heading }}
                </a>
            </td>
            
            <td>
                <a class="listcontrol" href=".?typ={{ typ }}&sort=owner{{ '' if sort != 'owner' or reverse else '&reverse=True' }}">
                    Owner {{ '' if pub else ' / Collaborators' }}
                </a>
            </td>

            <td>
                <a class="listcontrol" href=".?typ={{ typ }}{{ '' if sort != 'last_edited' or reverse else '&reverse=True' }}">
                    Last Edited
                </a>
            </td>
        </tr>
    </thead>
    <tbody>
        {% if not worksheets %}
        {% if pub %}
        <tr>
            <td colspan="5">
                There are no published worksheets.
            </td>
        </tr>
        {% elif typ == 'active' %}
        <tr>
            <td colspan="5" id="welcome-message">
                Welcome to Sage! You can <a href="/new_worksheet">create a new worksheet</a>,
                view <a href="/pub/">published worksheets</a>, or read the
                <a href="/help" target="_new">documentation</a>.
            </td>
        </tr>
        {% endif %}
        {% else %}

        {% for worksheet in worksheets %}
        {% set name = worksheet.filename() %}
        <tr>
            <td class="entry">
                {% if pub %}
                
                <a class="worksheet_edit" href="/home/{{ name }}/rating_info">
                    {% if worksheet.rating() < 0 %}
                    ----
                    {% else %}
                    {{ worksheet.rating() }}
                    {% endif %}
                </a>
                
                {% else %}
                
                <input type="checkbox" unchecked id="{{ name|css_escape }}" />
                {# I'm removing this select since it is a massive performance killer and these 
                serve no real purpose at all.  Plus google docs got rid of the analogous menu.
                <select onchange="go_option(this);" class="worksheet_edit">
                    <option value="" title="File options" selected>File</option>
                    <option value="list_rename_worksheet('{{ name }}','{{ worksheet.name() }}');" title="Change the name of this worksheet.">
                        Rename...
                    </option>            
                    <option value="list_edit_worksheet('{{ name }}');" title="Open this worksheet and edit it">Edit</option>
                    <option value="list_copy_worksheet('{{ name }}');" title="Copy this worksheet">Copy Worksheet</option>
                    <option value="list_share_worksheet('{{ name }}');" title="Share this worksheet with others">Collaborate</option>
                    <option value="list_publish_worksheet('{{ name }}');" title="Publish this worksheet on the internet">Publish</option>
                    <option value="list_revisions_of_worksheet('{{ name }}');" title="See all revisions of this worksheet">Revisions</option>
                </select>
                #}      
                {% endif %}
            </td>

            <td class="worksheet_link">
                <a title="{{ worksheet.name() | escape }}" id="name-{{ name|css_escape }}" class="worksheetname" href="/home/{{ name }}">
                    {% if worksheet.compute_process_has_been_started() %}(running) {% endif %}
                    {{ worksheet.truncated_name(50) | escape}}
                </a>
                
                {% if not pub and worksheet.is_published() %}(Published){% endif %}
            </td>
            <td class="owner_collab">

                {% if not pub %}
                {{ worksheet.owner() }}
                {% else %}
                {{worksheet.worksheet_that_was_published().owner()}}
                {% endif %}
                
                {% if not pub and typ != 'trash' %}

                {% set shared = False %}

                {% if worksheet.collaborator_names() %}
                / {{ worksheet.collaborator_names(5) }}
                {% set shared = True %}
                {% endif %}

                {% if worksheet.viewer_names() %}
                / {{ worksheet.viewer_names(5) }}
                {% set shared = True %}
                {% endif %}

                {% if (worksheet.owner() != username) or username == 'admin' %}
                {% set shared = False %}
                {% endif %}
                
                {% if shared %}
                <a class="share" href="/home/{{ worksheet.filename() }}/share">Add or Delete</a>
                {% else %}
                <a class="share" href="/home/{{ worksheet.filename() }}/share">Share now</a>
                {% endif %}

                {% if worksheet.has_published_version() %}
                <a href="/home/{{ worksheet.published_version().filename() }}">
                    (published)
                </a>
                {% endif %}
                
                {% endif %}
            </td>
            <td>
                {{ worksheet.html_time_since_last_edited() }}
            </td>
        </tr>
        {% endfor %}
        {% endif %}
    </tbody>
</table>
{% endblock %}
